﻿/*
 * 打印报价
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.BidSystem.Model;

public partial class Company_PrintPrice : System.Web.UI.Page, IAccess
{
    string action = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        CurrentAccount.CheckSessionTimeOut(this);

        // 验证报价密码
        if (!CurrentAccount.IsCheckBidPwd)
        {
            Response.Redirect(string.Format("CheckBidPwd.aspx?action={0}", action));
        }
        if (!CurrentAccount.IsConfirmBid)
        {
            Response.Redirect(string.Format("ConfirmBid.aspx?action={0}", action));
        }

        action = (Request.QueryString["action"] ?? string.Empty).ToLower();
        if (!IsPostBack)
        {
            BindData();
            HZ.BidSystem.BLL.BLL_Cache.SetOnlineModel(CurrentAccount.Account.AccountId.Value, new HZ.BidSystem.Model.OnLineModel() { Active = "正在打印报价" });
        }
    }
    private void BindData()
    {
        lblCompanyName.Text = CurrentAccount.OrgName;
        lblCompanyCode.Text = CurrentAccount.Account.AccountName;

        // 设置标题
        switch (action)
        {
            case "bargain":
                ltlInfo.Text = BLL_Factory.IBLL_Manage.GetConfigurationByKey("BargainPrintTitle").Value;
                break;
            case "compete":
                ltlInfo.Text = BLL_Factory.IBLL_Manage.GetConfigurationByKey("CompetePrintTitle").Value;
                break;
            default:
                if (CurrentAccount.IsDecPrice)
                {
                    ltlInfo.Text = BLL_Factory.IBLL_Manage.GetConfigurationByKey("BidDecPrintTitle").Value;
                }
                else
                {
                    ltlInfo.Text = BLL_Factory.IBLL_Manage.GetConfigurationByKey("BidPrintTitle").Value;
                }
                break;
        }
        switch (action)
        {
            case "bargain":
                BindBargain();
                break;
            case "compete":
                BindCompete();
                break;
            default:
                BindBid();
                break;
        }


    }

    private void BindCompete()
    {
        int totalrecord;
        var list = BLL_Factory.IBLL_Company.GetCompeteWithDec(new { SortFields = "Company_Goods", CompanyId_Tb = CurrentAccount.Account.AccountId, CompeteTimes = CurrentAccount.CurrentCompeteTimes }, 1, 0, out totalrecord, CurrentAccount.BidPwd, CurrentAccount.RandPwd);
        lblBidCount.Text = totalrecord.ToString();
        rp1.DataSource = list;
        rp1.DataBind();
    }
    private void BindBargain()
    {
        int totalrecord;
        var list = BLL_Factory.IBLL_Company.GetBargainWithDec(new { SortFields = "Company_Goods", CompanyId_Tb = CurrentAccount.Account.AccountId }, 1, 0, out totalrecord, CurrentAccount.BidPwd, CurrentAccount.RandPwd);
        lblBidCount.Text = totalrecord.ToString();
        rp1.DataSource = list;
        rp1.DataBind();
    }

    private void BindBid()
    {
        int totalrecord;
        var list = BLL_Factory.IBLL_Company.GetBidPriceWithDec(new { SortFields = "Company_Goods", CompanyId_Tb = CurrentAccount.Account.AccountId }, 1, 0, out totalrecord, CurrentAccount.BidPwd, CurrentAccount.RandPwd);
        lblBidCount.Text = totalrecord.ToString();
        rp1.DataSource = list;
        rp1.DataBind();
    }

    protected void rp1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Label lbl = e.Item.FindControl("lblbidprice") as Label;
            //Label lbl2 = e.Item.FindControl("lblbidprice2") as Label;
            Label lblFactor = e.Item.FindControl("lblFactor") as Label;

            switch (action)
            {
                case "bargain":
                    lbl.Text = (e.Item.DataItem as HZ.BidSystem.Model.View_BargainPrice).BargainDecPrice.GetValueOrDefault(0).ToString("F" + SystemConfig.Instance.Precision);
                    break;
                case "compete":
                    lbl.Text = (e.Item.DataItem as HZ.BidSystem.Model.View_CompetePrice).CompeteDecPrice.GetValueOrDefault(0).ToString("F" + SystemConfig.Instance.Precision);
                    break;
                default:

                    lbl.Text = (e.Item.DataItem as HZ.BidSystem.Model.View_BidPrice).DecPrice.GetValueOrDefault(0).ToString("F" + SystemConfig.Instance.Precision);

                    lblFactor.Text = (e.Item.DataItem as HZ.BidSystem.Model.View_BidPrice).PackFactor.GetValueOrDefault(0).ToString();  
                    //lbl2.Text = ((e.Item.DataItem as HZ.BidSystem.Model.View_BidPrice).DecPrice.GetValueOrDefault(0) / Convert.ToDecimal(lblFactor.Text)).ToString("F4");
                    
                    break;
            }
        }
    }
    #region IAccess Members

    IList<EnumAccess> IAccess.AccessList
    {
        get
        {
            var list = new List<EnumAccess>();
            list.Add(EnumAccess.Company);
            return list;
        }

    }

    #endregion
}
